package com.yjy.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.BaseUtils;
import com.system.comm.util.Snowflake;
import com.system.common.mapper.SysDeptMapper;
import com.system.common.mapper.SysUserMapper;
import com.system.common.pojo.SysDept;
import com.system.common.pojo.SysDeptExample;
import com.system.common.pojo.SysDeptExample.Criteria;
import com.system.common.pojo.SysUser;
import com.system.common.pojo.SysUserExample;

@Service
public class SysDeptServiceImpl implements SysDeptService {

	@Autowired
	private SysDeptMapper deptMapper;

	@Autowired
	private SysUserMapper userMapper;

	@Override
	public List<SysDept> listAll(Map<?, ?> map) {
		List<SysDept> depts = new ArrayList<>();
		String dname = (String) map.get("dname");
		SysDeptExample example = new SysDeptExample();
		Criteria cri = example.createCriteria();
		if (StringUtils.isNotBlank(dname)) {
			cri.andDeptNameLike("%" + dname + "%");
		}
		List<SysDept> list = deptMapper.selectByExample(example);
		for (SysDept dept : list) {
			if (dept.getIsDel().equals("0")) {
				depts.add(dept);
			}
		}
		return depts;
	}

	@Override
	public void del(String deptId) {
		SysDeptExample example = new SysDeptExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(deptId)) {
			criteria.andParentIdEqualTo(deptId);
		}
		criteria.andIsDelEqualTo("0");
		List<SysDept> list = deptMapper.selectByExample(example);
		if (list.isEmpty()) {
			SysDept dept = deptMapper.selectByPrimaryKey(deptId);
			dept.setIsDel("1");
			deptMapper.updateByPrimaryKey(dept);
			SysUserExample userExample = new SysUserExample();
			com.system.common.pojo.SysUserExample.Criteria userCriteria = userExample.createCriteria();
			userCriteria.andDeptIdEqualTo(deptId);
			List<SysUser> users = userMapper.selectByExample(userExample);
			if (users.size() > 0) {
				for (SysUser sysUser : users) {
					sysUser.setIsDel("1");
					userMapper.updateByPrimaryKey(sysUser);
				}
			}
		}
	}

	@Override
	public void save(SysDept dept) {
		Snowflake snowflake = new Snowflake(0, 0);
		// 通过雪花算法获取组织机构的id
		dept.setDeptId(snowflake.nextId() + "");
		dept.setIsDel("0");
		this.getCascadeId(dept);
		deptMapper.insert(dept);

	}

	private void getCascadeId(SysDept dept) {
		// 获取节点语义id
		String cascadeId = deptMapper.queryBycascadeId(dept.getParentId());// 通过当前要添加的节点的父节点去查询当前父节点的最大节点语义id
		if (cascadeId == null || cascadeId.equals("")) {// 如果等于null或者等于“”，说明当前添加的父节点没有子节点
			SysDept sys = deptMapper.selectByPrimaryKey(dept.getParentId());// 然后查询当前父节点的语义节点id
			dept.setCascadeId(sys.getCascadeId() + ".0001");// 在拼接字符串
		} else {
			String casc = BaseUtils.treeId(cascadeId, 123);
			dept.setCascadeId(casc);
		}

	}

	@Override
	public SysDept getById(String deptId) {
		SysDept dept = deptMapper.selectByPrimaryKey(deptId);
		if (dept != null) {
			return dept;
		}
		return null;
	}

	@Override
	public String selectParentname(String parentId) {
		SysDept sysDept = deptMapper.selectByPrimaryKey(parentId);
		if (sysDept != null) {
			return sysDept.getDeptName();
		}
		return "";
	}

	@Override
	public void update(SysDept dept) {
		this.getCascadeId(dept);
		SysDept sysDept = deptMapper.selectByPrimaryKey(dept.getDeptId());
		dept.setCreateUserId(sysDept.getCreateUserId());
		dept.setCreateTime(sysDept.getCreateTime());
		dept.setIsDel("0");
		deptMapper.updateByPrimaryKey(dept);

	}

	@Override
	public void move(String deptIds, String parentIds) {
		SysDept dept = deptMapper.selectByPrimaryKey(deptIds);
		dept.setParentId(parentIds);
		this.getCascadeId(dept);
		deptMapper.updateByPrimaryKey(dept);

	}

	@Override
	public List<SysDept> queryDeptIds(String ids) {
		List<SysDept>  depts=new ArrayList<>();
		if (ids.equals("0")) {
			SysDeptExample example=new SysDeptExample();
			Criteria criteria=example.createCriteria();
			criteria.andIsDelEqualTo("0");
			 depts=deptMapper.selectByExample(example);
		}else {
			 depts = deptMapper.queryDeptIds(ids);
		}
		
		return depts;
	}

}
